package tech.eyao.hs60x.user.controller;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import tech.eyao.hs60x.annotation.Admin;
import tech.eyao.hs60x.awareUtil.CommonServiceUtil;
import tech.eyao.hs60x.dto.ApprovalDTO;
import tech.eyao.hs60x.dto.BaseResult;
import tech.eyao.hs60x.enums.AccountType;
import tech.eyao.hs60x.model.domain.AccountDO;
import tech.eyao.hs60x.service.ApprovalService;
import tech.eyao.hs60x.utils.TokenUtils;
import tech.eyao.hs60x.vo.ApprovalPageQuery;

@RestController
@RequestMapping("/apply")
@Api("绑定审批相关")
public class ApprovalController {
    private static final Logger logger = LoggerFactory.getLogger(ApprovalController.class);
    @Autowired
    private ApprovalService approvalService;

    @Admin
    @GetMapping("/list.do")
    @ApiOperation(value = "审批列表", response = ApprovalDTO.class)
    public BaseResult list(@ApiParam(name="查询对象",value="json格式",required=true) ApprovalPageQuery pageQuery) {
        pageQuery.setUserId(TokenUtils.getUserId());
        return approvalService.getListByPage(pageQuery);
    }

    @Admin
    @PostMapping("/agree.do")
    @ApiOperation(value = "审批申请")
    public BaseResult agree(@ApiParam("id") Long id,@ApiParam("允许|拒绝：ALLOW|REFUSE") String action) {
        if(StringUtils.isEmpty(action)) {
            return BaseResult.isBussinessFail("审批授权参数缺少,请确认Action!");
        }
        return approvalService.agree(id, TokenUtils.getUserId(),action);
    }
    
    @PostMapping("/checkDeviceIsBind.do")
    @ApiOperation(value = "校验终端设备是否已被绑定")
    public @ResponseBody BaseResult checkDeviceIsBind(@ApiParam("phone(非必填)") String phone,
                                        @ApiParam("deviceId(IMEI)") String deviceId) {
        Map<String,Object> keyMap = new HashMap<String,Object>();
        if (StringUtils.isEmpty(deviceId)) {
            return BaseResult.isBussinessFail("参数有误,请传入设备IMEI|设备号");
        }
        try {
            AccountDO accountDO = new AccountDO();
            accountDO.setDeviceId(deviceId);
            accountDO.setPhone(phone);
            accountDO.setAccountType(AccountType.WATCH.getValue());
            boolean firstBindState = CommonServiceUtil.getInstance().checkDeviceIsBind(accountDO);
            keyMap.put("isBind", firstBindState);
            keyMap.put("currisBind", CommonServiceUtil.getInstance().isBindWatch(TokenUtils.getUserId(), deviceId, phone));
            keyMap.put("deviceIdStat", Boolean.TRUE);
            return BaseResult.isSuccess(JSON.toJSON(keyMap));
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return BaseResult.isDeFaultNoData();
    }
    
}
